<template>
	<view class="container">
		<!-- Loading状态 -->
		<view class="loading-state" v-if="loading">
			<uni-load-more status="loading" :content-text="loadingText"></uni-load-more>
		</view>
		
		<!-- 空状态 -->
		<view class="empty-state" v-else-if="contacts.length === 0">
			<!-- <image src="/static/images/empty.png" mode="aspectFit" class="empty-image"></image> -->
			<text class="empty-text">暂无联系方式</text>
		</view>
		
		<!-- 联系方式列表 -->
		<view class="contact-list" v-else>
			<view 
				class="contact-item" 
				v-for="contact in contacts"
				:key="contact.id"
				@tap="showQRCode(contact)"
			>
				<view class="item-left">
					<text class="iconfont contact-icon" :class="getIconClass(contact.contact_way)"></text>
					<view class="contact-info">
						<text class="contact-name">{{ contact.contact_way }}</text>
						<text class="contact-desc">{{ contact.contact_content }}</text>
					</view>
				</view>
				<uni-icons type="right" size="16" color="#999"></uni-icons>
			</view>
		</view>
		
		<!-- 二维码弹窗 -->
		<uni-popup ref="qrPopup" type="center">
			<view class="qr-popup">
				<view class="popup-header">
					<text class="popup-title">{{ currentQRTitle }}</text>
					<text class="close-btn" @tap="closeQRCode">×</text>
				</view>
				<view class="popup-content">
					<image 
						:src="api.BASE_URL + currentQRCode" 
						mode="aspectFit" 
						class="qr-image"
						@longpress="saveQRCode"
					></image>
					<text class="qr-tip">长按保存二维码</text>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { api } from '@/utils/request'

const qrPopup = ref(null)
const currentQRCode = ref('')
const currentQRTitle = ref('')
const contacts = ref([])
const loading = ref(true)

// 获取联系方式列表
const getContacts = async () => {
	try {
		loading.value = true
		const response = await api.request({
			url: '/api/system/get_contact_us/',
			method: 'GET',
			data: {}
		})
		
		if(response.code === 200) {
			contacts.value = response.data
		} else {
			throw new Error(response.message)
		}
	} catch(error) {
		console.error('获取联系方式失败:', error)
		uni.showToast({
			title: '获取联系方式失败',
			icon: 'none'
		})
	} finally {
		loading.value = false
	}
}

// 显示二维码
const showQRCode = (contact) => {
	currentQRTitle.value = contact.contact_way
	currentQRCode.value = contact.contact_qr
	qrPopup.value.open()
}

// 关闭二维码
const closeQRCode = () => {
	qrPopup.value.close()
}

// 保存二维码
const saveQRCode = () => {
	// 先下载图片到本地临时路径
	uni.downloadFile({
		url: api.BASE_URL + currentQRCode.value,
		success: (res) => {
			// 再保存到相册
			uni.saveImageToPhotosAlbum({
				filePath: res.tempFilePath,
				success: () => {
					uni.showToast({
						title: '保存成功',
						icon: 'success'
					})
				},
				fail: (err) => {
					console.error('保存失败:', err)
					uni.showToast({
						title: '保存失败',
						icon: 'none'
					})
				}
			})
		},
		fail: (err) => {
			console.error('下载失败:', err)
			uni.showToast({
				title: '下载失败',
				icon: 'none'
			})
		}
	})
}

// 获取图标类名
const getIconClass = (contactWay) => {
	const way = contactWay.toLowerCase()
	if(way.includes('qq')) return 'icon-qq'
	if(way.includes('微信')) return 'icon-weixin'
	if(way.includes('支付宝')) return 'icon-zhifubaozhifu'
	return 'icon-qita' // 默认图标
}

// 加载提示文本
const loadingText = {
	contentdown: '上拉显示更多',
	contentrefresh: '正在加载...',
	contentnomore: '没有更多数据了'
}

onMounted(() => {
	getContacts()
})
</script>

<style lang="scss" scoped>
.container {
	min-height: 100vh;
	background: #f8f9fa;
	padding: 20rpx;
}

.contact-list {
	background: #fff;
	border-radius: 16rpx;
	overflow: hidden;
	
	.contact-item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 30rpx;
		border-bottom: 1rpx solid #ebeef5;
		
		&:last-child {
			border-bottom: none;
		}
		
		&:active {
			background: #f8f8f8;
		}
		
		.item-left {
			display: flex;
			align-items: center;
			gap: 20rpx;
			
			.contact-icon {
				font-size: 48rpx;
				
				&.icon-qq {
					color: #12B7F5;
				}
				
				&.icon-weixin {
					color: #07C160;
				}
				
				&.icon-zhifubaozhifu {
					color: #1677FF;
				}
			}
			
			.contact-info {
				.contact-name {
					font-size: 32rpx;
					color: #333;
					margin-bottom: 6rpx;
				}
				
				.contact-desc {
					font-size: 24rpx;
					color: #999;
				}
			}
		}
	}
}

.qr-popup {
	width: 600rpx;
	background: #fff;
	border-radius: 20rpx;
	overflow: hidden;
	
	.popup-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 30rpx;
		border-bottom: 1rpx solid #ebeef5;
		
		.popup-title {
			font-size: 32rpx;
			font-weight: 600;
			color: #333;
		}
		
		.close-btn {
			font-size: 40rpx;
			color: #999;
			padding: 10rpx;
		}
	}
	
	.popup-content {
		padding: 40rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		
		.qr-image {
			width: 400rpx;
			height: 400rpx;
			margin-bottom: 20rpx;
		}
		
		.qr-tip {
			font-size: 24rpx;
			color: #999;
		}
	}
}

.loading-state {
	display: flex;
	justify-content: center;
	align-items: center;
	padding: 20rpx;
}

.empty-state {
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;
	padding: 20rpx;
}

.empty-image {
	width: 200rpx;
	height: 200rpx;
	margin-bottom: 20rpx;
}

.empty-text {
	font-size: 24rpx;
	color: #999;
}
</style>
